<?php

namespace Biz\model\brand;

use Biz\cache\brand\Brand as cBrand;
use Doraemon\pockets\datebase\BaseDbModel;

/**
 * 品牌付费套餐表
 *
 * @category   Module name
 * @package    PSR
 * @subpackage Documentation\API
 * @author     zhouyang  <zhouyang@haohaozhu.com>
 * @license    GPL http://www.haohaozhu.com
 * @link       http://www.haohaozhu.com
 */
class BrandPackage extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_store';

    const _CONFIG_SELECT_ = 'db_store';

    const _DATABASE_ = 'store';

    const _TABLE_NAME = 'brand_package';

    const _SUB_TABLE = false;

    // 套餐状态Arr
    const STATUS_ARR = [
        self::STATUS_NORMAL => "启用",
        self::STATUS_OFF => "禁用",
        self::STATUS_DELETE => "删除",
    ];

    // 套餐状态（1：启用；9：删除 0：禁用）
    const STATUS_NORMAL = 1;
    const STATUS_DELETE = 9;
    const STATUS_OFF = 0;

    //基础版
    const PACK_TYPE_NORMAL = 1;
    // 高级版
    const PACK_TYPE_PLUS = 2;
    // 高级pro版
    const PACK_TYPE_PRO = 3;

    //新套餐
    //认证账号
    const PACK_CERTIFIED = 4;
    //高级认证账号
    const PACK_HIGH_CERTIFIED = 5;
    //认证套餐
    const PACK_CERTIFIED_PLUS = 6;
    //高级认证套餐
    const PACK_HIGH_CERTIFIED_PLUS = 7;
    //高级认证简单套餐
    const PACK_HIGH_CERTIFIED_SIMPLE = 8;

    //自定义的套餐PACK_TYPE 10000起
    const NEW_PACK_TYPE_START = 10000;

    //线上
    const TYPE_ONLINE = 1;
    //线下
    const TYPE_OFFLINE = 2;


    public static $types = [
        self::TYPE_ONLINE => '线上套餐',
        self::TYPE_OFFLINE => '线下自定义套餐',
    ];


    public static $packTypeList = [
        self::PACK_TYPE_NORMAL,
        self::PACK_TYPE_PLUS,
        self::PACK_TYPE_PRO,
    ];

    //新品牌套餐
    public static $newPackTypeList = [
        self::PACK_CERTIFIED,
        self::PACK_HIGH_CERTIFIED,
        self::PACK_CERTIFIED_PLUS,
        self::PACK_HIGH_CERTIFIED_PLUS,
        self::PACK_HIGH_CERTIFIED_SIMPLE,
    ];


    public static $packTypeDesc = [
        self::PACK_TYPE_NORMAL => '基础版套餐',
        self::PACK_TYPE_PLUS => '高级版套餐',
        self::PACK_TYPE_PRO => '高级Pro版套餐',
        self::PACK_CERTIFIED => '认证账号',
        self::PACK_HIGH_CERTIFIED => '高级认证账号',
        self::PACK_CERTIFIED_PLUS => '认证套餐',
        self::PACK_HIGH_CERTIFIED_PLUS => '高级认证套餐',
        self::PACK_HIGH_CERTIFIED_SIMPLE => '高级认证简单套餐'
    ];

    public static $newPackTypeDesc = [
        self::PACK_CERTIFIED => '认证账号',
        self::PACK_HIGH_CERTIFIED => '高级认证账号',
        self::PACK_CERTIFIED_PLUS => '认证套餐',
        self::PACK_HIGH_CERTIFIED_PLUS => '高级认证套餐',
        self::PACK_HIGH_CERTIFIED_SIMPLE => '高级认证简单套餐',
    ];

    public static $packWeight = [
        self::PACK_TYPE_NORMAL => 0,
        self::PACK_TYPE_PLUS => 1,
        self::PACK_TYPE_PRO => 2
    ];

    public static $newPackWeight = [
        self::PACK_CERTIFIED => 0,
        self::PACK_HIGH_CERTIFIED => 1,
        self::PACK_CERTIFIED_PLUS => 2,
        self::PACK_HIGH_CERTIFIED_SIMPLE => 3,
        self::PACK_HIGH_CERTIFIED_PLUS => 4
    ];

    //套餐升级规则
    public static $packUpgrade = [
        self::PACK_TYPE_NORMAL => [
            self::PACK_TYPE_PLUS,
            self::PACK_TYPE_PRO,
        ],
        self::PACK_TYPE_PLUS => [
            self::PACK_TYPE_PRO,
        ],
        self::PACK_TYPE_PRO => [
        ]
    ];

    //新套餐升级规则
    public static $newPackUpgrade = [
        self::PACK_CERTIFIED => [
            self::PACK_HIGH_CERTIFIED,
            self::PACK_CERTIFIED_PLUS,
            self::PACK_HIGH_CERTIFIED_PLUS
        ],
        self::PACK_HIGH_CERTIFIED => [
            self::PACK_HIGH_CERTIFIED_SIMPLE,
            self::PACK_HIGH_CERTIFIED_PLUS
        ],
        self::PACK_CERTIFIED_PLUS => [
            self::PACK_HIGH_CERTIFIED_PLUS
        ],
        self::PACK_HIGH_CERTIFIED_SIMPLE => [
            self::PACK_HIGH_CERTIFIED_PLUS
        ],
        self::PACK_HIGH_CERTIFIED_PLUS => [
        ]
    ];

    //统计类型
    const STAT_PACK_TRY = 1;
    const STAT_PACK_CERTIFIED_PLUS = 2;
    const STAT_PACK_HIGH_CERTIFIED_PLUSY = 3;
    public static $statPackTypes = [
        self::STAT_PACK_TRY => '体验套餐',
        self::STAT_PACK_CERTIFIED_PLUS => '认证套餐',
        self::STAT_PACK_HIGH_CERTIFIED_PLUSY => '高级认证套餐',
    ];

    public static function getById($id)
    {
        return self::getInfoByWhere(['id' => $id]);
    }

    public static function getInfoByPackType($packType)
    {
        return self::getInfoByWhere([
            'package_status' => self::STATUS_NORMAL,
            'pack_type' => $packType,
        ]);
    }


    /**
     * 根据条件获取套餐列表
     * @param $where
     * @param string $field
     * @return array
     */
    public static function getListByWhere($where, $field = "*"): array
    {
        return self::selectListByWhere($where, $field);
    }

    /**
     * 获取所有套餐
     * @return array|bool
     */
    public static function getAll()
    {
        return self::selectListByWhere([
            'package_status' => self::STATUS_NORMAL,
        ]);
    }

    public static function getInfoByWhere($where, $field = '*')
    {
        $info = self::get($field, $where);
        $info = self::formatInfo($info);
        return $info;
    }

    public static function selectListByWhere($where, $field = '*')
    {
        $res = self::select($field, $where);
        $list = [];
        foreach ($res as $row) {
            $info = self::formatInfo($row);
            $list[] = $info;
        }
        return $list;
    }

    public static function getPackList()
    {
        $res = self::selectListByWhere([
            'package_status' => self::STATUS_NORMAL,
            'pack_type' => self::$packTypeList,
        ]);
        $sortWeight = array_column($res, 'pack_weight');
        array_multisort($sortWeight, SORT_ASC, $res);
        $list = array_column($res, null, 'pack_type');
        return $list;
    }

    public static function getNewPackList($currentPackType)
    {
        $arr = self::selectListByWhere([
            'package_status' => self::STATUS_NORMAL,
            'pack_type' => self::$newPackTypeList,
        ]);
        $res = [];
        foreach ($arr as $row) {
            $row['pack_weight'] = self::$newPackWeight[$row['pack_type']];
            $res[] = $row;
        }
        $sortWeight = array_column($res, 'pack_weight');
        array_multisort($sortWeight, SORT_ASC, $res);
        $list = array_column($res, null, 'pack_type');
        //套餐改版2.0.如果是认证账号、认证套餐(4,6)，则返回3，4，5，6套餐。否则，不返回认证账号、认证套餐，增加高级认证基础套餐 返回(5,7,8)
        if (in_array($currentPackType, [self::PACK_CERTIFIED, self::PACK_CERTIFIED_PLUS])) {
            unset($list[self::PACK_HIGH_CERTIFIED_SIMPLE]);
        } else {
            unset($list[self::PACK_CERTIFIED]);
            unset($list[self::PACK_CERTIFIED_PLUS]);
        }
        return $list;
    }

    private static function formatInfo($info)
    {
        if ($info) {
            $info['pack_weight'] = $info['pack_type'] ? (isset(self::$packWeight[$info['pack_type']]) ? self::$packWeight[$info['pack_type']] : 0) : 0;
        }
        return $info;
    }


    public static function updateData($data, $where)
    {
        if (!isset($data['edit_time'])) {
            $data['edit_time'] = time();
        }
        $ret = static::update($data, $where);
        if ($ret->errorCode() !== '00000') {
            return false;
        }
        return true;
    }

    public static function insertData($data)
    {
        if (!isset($data['add_time'])) {
            $data['add_time'] = time();
        }
        if (!isset($data['edit_time'])) {
            $data['edit_time'] = time();
        }
        $ret = self::insert($data);
        if ($ret->errorCode() !== '00000') {
            return false;
        }
        return self::id();
    }

    /**
     * 获取新的pack_type
     * @param $type
     * @return int
     */
    public static function getNewPackType()
    {
        $lockKey = 'brand:package:pack_type';
        if (cBrand::isLock($lockKey, 15)) {
            return false;
        }
        $where['pack_type[>=]'] = self::NEW_PACK_TYPE_START;
        $where['ORDER'] = [
            'pack_type' => 'DESC',
        ];
        $lastPackType = self::get('pack_type', $where);
        if (!$lastPackType) {
            return self::NEW_PACK_TYPE_START;
        }
        $packType = ++$lastPackType;
        return $packType;
    }

    public static function getNewCreatedPackage(){

        $where['package_status'] = self::STATUS_NORMAL;
        $where['pack_type[>=]'] = self::NEW_PACK_TYPE_START;
        $where['ORDER'] = [
            'pack_type' => 'ASC'
        ];
        $arr = self::selectListByWhere($where);
        return $arr;
    }

    public static function getPackageByPackType($packType){
        if($packType < self::NEW_PACK_TYPE_START){
            return self::$packTypeDesc[$packType];
        }
        $package = self::getInfoByPackType($packType);
        if($package){
            return $package['package_name'];
        }
        return '';
    }

    public static function allPackageTypes(){
        $oldPackTypes = self::$packTypeDesc;
        $newPackages = [];
        $result = self::getNewCreatedPackage();
        if($result && is_array($result)){
            $newPackages = array_column($result,'package_name','pack_type');
        }
        return $oldPackTypes + $newPackages;
    }

}
